package org.jix.weblog.module.admin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.jix.weblog.module.admin.model.vo.file.UploadFileRspVO;
import org.jix.weblog.module.admin.service.AdminFileService;
import org.jix.weblog.module.common.enums.ResponseCodeEnum;
import org.jix.weblog.module.common.exception.BizException;
import org.jix.weblog.module.common.utils.MinioUtil;
import org.jix.weblog.module.common.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @version 1.0
 * @Author Jix
 * @Date 2024/9/7 11:23
 * @注释
 */

@Service
@Slf4j
public class AdminFileServiceImpl implements AdminFileService {
    @Autowired
    private MinioUtil minioUtil;

    /**
     * 上传文件
     * @param file
     * @return
     */
    @Override
    public Response uploadFile(MultipartFile file) {
        try {
            //上传文件
            String url = minioUtil.uploadFile(file);
            //构建反参，返回访问链接
            return Response.success(UploadFileRspVO.builder().url(url).build());
        }catch (Exception e) {
            log.error("啊......似乎出了点小问题",e);
            // 手动抛出业务异常，提示 “文件上传失败”
            throw new BizException(ResponseCodeEnum.FILE_UPLOAD_FAILED);
        }
    }
}
